package com.zzj.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zzj.base.Result;
import com.zzj.entity.Catheter;
import com.zzj.entity.Extubation;
import com.zzj.entity.Maintain;
import com.zzj.entity.PatientAppointment;
import com.zzj.model.dto.treat.CatheterCreateDTO;
import com.zzj.model.dto.treat.ExtubationCreateDTO;
import com.zzj.model.dto.treat.MaintainCreateDTO;
import com.zzj.service.ICatheterService;
import com.zzj.service.IExtubationService;
import com.zzj.service.IMaintainService;
import com.zzj.service.IPatientAppointmentService;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 置管 前端控制器
 * </p>
 *
 * @author zzj
 * @since 2023-03-10
 */
@RestController
@RequestMapping("/treat")
public class TreatController {

    @Resource
    private ICatheterService catheterService;
    @Resource
    private IExtubationService extubationService;
    @Resource
    private IMaintainService maintainService;

    @Resource
    private IPatientAppointmentService appointmentService;


    @Transactional
    @PostMapping("catheterCreate")
    public Result catheterCreate(@RequestBody @Validated CatheterCreateDTO in){
        Catheter catheter=new Catheter();
        catheter.setPatientUsername(in.getPatientUsername());
        catheter.setPatientName(in.getPatientName());
        catheter.setNurseUsername(in.getNurseUsername() );
        catheter.setNurseName(in.getNurseName());
        catheter.setDisease(in.getDisease());
        catheter.setFirstCatheter(in.getFirstCatheter());
        catheter.setCatheterOpportunity(in.getCatheterOpportunity());
        catheter.setCatheterSpecification(in.getCatheterSpecification());
        catheter.setCatheterLength(in.getCatheterLength());
        catheter.setPunctureMethod(in.getPunctureMethod());
        catheter.setCatheterSite(in.getCatheterSite());
        catheter.setCatheterArm(in.getCatheterArm());
        catheter.setCatheterVein(in.getCatheterVein());
        catheter.setCatheterTipPosition(in.getCatheterTipPosition());
        catheter.setCatheterInsertionLength(in.getCatheterInsertionLength());
        catheter.setCatheterExposedLength(in.getCatheterExposedLength());
        catheter.setImages(in.getImages());
        boolean save = catheterService.save(catheter);
        boolean update = appointmentService.update(new PatientAppointment(),
                new LambdaUpdateWrapper<PatientAppointment>()
                        .eq(PatientAppointment::getId, in.getAppointmentId())
                        .set(PatientAppointment::getTreatId, catheter.getId())
                        .set(PatientAppointment::getStatus,3));
        if(!save||!update){
            return Result.error("上传失败");
        }
        return Result.success("上传成功");
    }
    @Transactional
    @PostMapping("maintainCreate")
    public Result maintainCreate(@RequestBody @Validated MaintainCreateDTO in){

        Maintain maintain =new Maintain();
        maintain.setPatientUsername(in.getPatientUsername());
        maintain.setPatientName(in.getPatientName());
        maintain.setNurseUsername(in.getNurseUsername() );
        maintain.setNurseName(in.getNurseName());
        maintain.setPipelineUnobstructed(in.getPipelineUnobstructed());
        maintain.setChangeDressing(in.getChangeDressing());
        maintain.setReplaceConnector(in.getReplaceConnector());
        maintain.setResistance(in.getResistance());
        maintain.setCompanion(in.getCompanion() );
        maintain.setBloodReturn(in.getBloodReturn());
        maintain.setCatheterExposedLength(in.getCatheterExposedLength());
        maintain.setImages(in.getImages());
        boolean save = maintainService.save(maintain);
        boolean update = appointmentService.update(new PatientAppointment(),
                new LambdaUpdateWrapper<PatientAppointment>()
                        .eq(PatientAppointment::getId, in.getAppointmentId())
                        .set(PatientAppointment::getTreatId, maintain.getId())
                        .set(PatientAppointment::getStatus,3));

        if(!save||!update){
            return Result.error("上传失败");
        }
        return Result.success("上传成功");


    }
    @Transactional
    @PostMapping("extubationCreate")
    public Result extubationCreate(@RequestBody @Validated ExtubationCreateDTO in){

        Extubation extubation =new Extubation();
        extubation.setPatientUsername(in.getPatientUsername());
        extubation.setPatientName(in.getPatientName());
        extubation.setNurseUsername(in.getNurseUsername() );
        extubation.setNurseName(in.getNurseName());
        extubation.setIndwellingTime(in.getIndwellingTime());
        extubation.setEndIntact(in.getEndIntact());
        extubation.setProcessSmooth(in.getProcessSmooth());
        extubation.setImages(in.getImages());
        boolean save = extubationService.save(extubation);
        boolean update = appointmentService.update(new PatientAppointment(),
                new LambdaUpdateWrapper<PatientAppointment>()
                        .set(PatientAppointment::getTreatId, extubation.getId())
                        .set(PatientAppointment::getStatus,3)
                        .set(PatientAppointment::getStatus,3));
        if(!save||!update){
            return Result.error("上传失败");
        }
        return Result.success("上传成功");
    }


    @Transactional
    @PostMapping("queryByIdType")
    public Result id(@RequestParam int id,@RequestParam int type){
        QueryWrapper queryWrapper =new QueryWrapper();
        queryWrapper.eq("id",id);
        if(type==1){
            Maintain maintain = maintainService.getBaseMapper().selectOne(queryWrapper);
            return Result.success(maintain);
        }else if(type==2){
            Catheter catheter = catheterService.getBaseMapper().selectOne(queryWrapper);
            return Result.success(catheter);
        }else if(type==3){
            Extubation extubation = extubationService.getBaseMapper().selectOne(queryWrapper);
            return Result.success(extubation);
        }
        return Result.error("查询失败");
    }
}
